package cn.humaohua.study.hadoop.cmdrunner.c5;

import cn.humaohua.study.hadoop.cmdrunner.Test;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.WritableComparator;
import org.springframework.stereotype.Component;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;

@Component
public class TextPairTest implements Test {
    @Override
    public void test() throws Exception {
        TextPair t1 = new TextPair("aa", "ab");
        TextPair t2 = new TextPair("aa", "ab");
        TextPair t3 = new TextPair("aa", "aa");

        System.out.println(String.format("t1.equals(t2)=%s", t1.equals(t2)));
        System.out.println(String.format("t1.compareTo(t3)=%s", t1.compareTo(t3)));

        WritableComparator comparator = new TextPair.Comparator();
        //调用TextPair自身的compareTo
        System.out.println(String.format("comparator.compare(t1,t2)=%s", comparator.compare(t1, t2)));

        ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
        DataOutputStream dos1 = new DataOutputStream(bos1);
        t1.write(dos1);

        ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
        DataOutputStream dos2 = new DataOutputStream(bos2);
        t2.write(dos2);

        byte[] tp1 = bos1.toByteArray();
        byte[] tp2 = bos2.toByteArray();
        System.out.println(String.format("comparator.compare(tp1, 0, tp1.length, tp2, 0, tp2.length)=%s", comparator.compare(tp1, 0, tp1.length, tp2, 0, tp2.length)));

        ByteArrayOutputStream bos3 = new ByteArrayOutputStream();
        DataOutputStream dos3 = new DataOutputStream(bos3);
        t3.write(dos3);

        byte[] tp3 = bos3.toByteArray();
        System.out.println(String.format("comparator.compare(tp1, 0, tp1.length, tp3, 0, tp3.length)=%s", comparator.compare(tp1, 0, tp1.length, tp3, 0, tp3.length)));

        IOUtils.closeStream(dos1);
        IOUtils.closeStream(bos1);

        IOUtils.closeStream(dos2);
        IOUtils.closeStream(bos2);

        IOUtils.closeStream(dos3);
        IOUtils.closeStream(bos3);

    }
}
